package com.supervision.parent;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.AsyncTask;
import android.os.IBinder;
import android.widget.Toast;

import com.supervision.parent.util.SaveNum;

import java.net.URL;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SOSService extends Service {
    String phone, webIp;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        phone = intent.getStringExtra("phone");
        webIp = intent.getStringExtra("webIp");
        return super.onStartCommand(intent, flags, startId);
    }

    public SOSService() {

        SOSAT sosat = new SOSAT();
        sosat.execute();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    class SOSAT extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onProgressUpdate(Void... values) {

        }

        @Override
        protected Void doInBackground(Void... voids) {
            OkHttpClient okHttpClient = new OkHttpClient();//创建单例
            URL url;
            Request request;//创建请求
            String result = null;
            while (true) {
                try {
                    Thread.sleep(2000);
                    //设置url
                    url = new URL("http://" + webIp + ":8080/getSOS/" + phone);
                    //设置请求
                    request = new Request.Builder()
                            .url(url)
                            .build();
                    //执行请求
                    Response response = okHttpClient.newCall(request).execute();

                    result = response.body().string();
                    if (result != null) {
                        //onProgressUpdate();
                        System.out.println(result);
                    }
                } catch (Exception e) {
                }
            }

        }
    }
}
